<template>
	<div>
		<uni-search-bar v-model="sv" placeholder="搜索" @confirm="doSearch"></uni-search-bar>
		<uni-list>
			<uni-list-item v-for="(s, i) in list" :title="s[appInfo.title_field]" v-if="appInfo.note_field"
			clickable :rightText="s[appInfo.right_field]" :note="s[appInfo.note_field]"
				@click="showDetail(s)" showArrow></uni-list-item>
				
			<uni-list-item v-for="(s, i) in list" :title="s[appInfo.title_field]"
				clickable :rightText="s[appInfo.right_field]" v-if="!appInfo.note_field"
				@click="showDetail(s)" showArrow></uni-list-item>
		</uni-list>
	</div>
</template>

<script>
	import { apiGet, apiDs	} from '@/api/openapi.js'
	import {getDicts, selectDictLabel	} from '@/api/sys.js'
	import {getApp	} from '@/api/app.js'
	export default {
		data() {
			return {
				appCode: null,
				appInfo: {},
				list: [],
				total: 0,
				sv:null,
				pager:{
					page:1, 
					limit:10,
				}
			}
		},
		onLoad(option) {
			this.appCode = option.app;
			this.initApp();
		},
		onShow() {			
			this.doSearch();
		},
		onPullDownRefresh() {
			this.loadList();
			setTimeout(function () {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			initApp() {
				getApp(this.appCode).then(response => {
					this.appInfo = response.data;
					this.loadList();
				});
			},
			loadList() {				
				if(!this.appInfo.api){
					return;
				}
				let params = {... this.pager};
				if(this.sv){
					params['keyword'] = this.sv
				};
				console.log(this.appInfo)
				apiDs(this.appInfo.api, params).then(response => {
					this.list = response.data;
				});
			},
			doSearch() {
				this.pager.page = 1;
				this.loadList();
			},
			cancelSerach() {
				this.doSearch();
				this.sv = null;
			},
			showDetail(s) {
				if(!this.appInfo.links){
					return;
				}
				let url = this.appInfo.links;
				if(url.indexOf('?')>0){
					url += '&id='+s.id
				}else{
					url += '?id='+s.id
				}
				uni.navigateTo({
					url: url
				});
			}
		}
	}
</script>

<style>

</style>
